package com.fuxi.cloud.common.util;

import io.minio.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.net.URLDecoder;

/**
 * MinioUtil
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class MinioUtil {
    private static final Logger log = LoggerFactory.getLogger(MinioUtil.class);
    private static String minioUrl;
    private static String minioName;
    private static String minioPass;
    private static String bucketName;
    private static MinioClient minioClient = null;

    public MinioUtil() {
    }

    public static void setMinioUrl(String minioUrl) {
        minioUrl = minioUrl;
    }

    public static void setMinioName(String minioName) {
        minioName = minioName;
    }

    public static void setMinioPass(String minioPass) {
        minioPass = minioPass;
    }

    public static void setBucketName(String bucketName) {
        bucketName = bucketName;
    }

    public static String getMinioUrl() {
        return minioUrl;
    }

    public static String getBucketName() {
        return bucketName;
    }

    public static String upload(MultipartFile file, String bizPath, String customBucket) {
        String var3 = "";
        bizPath = CommonUtils.filter(bizPath);
        String var4 = bucketName;
        if (oConvertUtils.isNotEmpty(customBucket)) {
            var4 = customBucket;
        }

        try {
            initMinio(minioUrl, minioName, minioPass);
            if (minioClient.bucketExists((BucketExistsArgs)((BucketExistsArgs.Builder)BucketExistsArgs.builder().bucket(var4)).build())) {
                log.debug("Bucket already exists.");
            } else {
                minioClient.makeBucket((MakeBucketArgs)((io.minio.MakeBucketArgs.Builder)MakeBucketArgs.builder().bucket(var4)).build());
                log.debug("create a new bucket.");
            }

            InputStream var5 = file.getInputStream();
            String var6 = file.getOriginalFilename();
            if ("".equals(var6)) {
                var6 = file.getName();
            }

            var6 = CommonUtils.getFileName(var6);
            String var7 = bizPath + "/" + var6.substring(0, var6.lastIndexOf(".")) + "_" + System.currentTimeMillis() + var6.substring(var6.indexOf("."));
            if (var7.startsWith("/")) {
                var7 = var7.substring(1);
            }

            PutObjectArgs var8 = (PutObjectArgs)((io.minio.PutObjectArgs.Builder)((io.minio.PutObjectArgs.Builder)PutObjectArgs.builder().object(var7)).bucket(var4)).contentType("application/octet-stream").stream(var5, (long)var5.available(), -1L).build();
            minioClient.putObject(var8);
            var5.close();
            var3 = minioUrl + var4 + "/" + var7;
        } catch (Exception var9) {
            log.error(var9.getMessage(), var9);
        }

        return var3;
    }

    public static String upload(MultipartFile file, String bizPath) {
        return upload(file, bizPath, (String)null);
    }

    public static InputStream getMinioFile(String bucketName, String objectName) {
        GetObjectResponse var2 = null;

        try {
            initMinio(minioUrl, minioName, minioPass);
            GetObjectArgs var3 = (GetObjectArgs)((io.minio.GetObjectArgs.Builder)((io.minio.GetObjectArgs.Builder)GetObjectArgs.builder().object(objectName)).bucket(bucketName)).build();
            var2 = minioClient.getObject(var3);
        } catch (Exception var4) {
            log.error("文件获取失败" + var4.getMessage(), var4);
        }

        return var2;
    }

    public static void removeObject(String bucketName, String objectName) {
        try {
            initMinio(minioUrl, minioName, minioPass);
            RemoveObjectArgs var2 = (RemoveObjectArgs)((io.minio.RemoveObjectArgs.Builder)((io.minio.RemoveObjectArgs.Builder)RemoveObjectArgs.builder().object(objectName)).bucket(bucketName)).build();
            minioClient.removeObject(var2);
        } catch (Exception var3) {
            log.error("文件删除失败" + var3.getMessage(), var3);
        }

    }

    public static String getObjectURL(String bucketName, String objectName, Integer expires) {
        initMinio(minioUrl, minioName, minioPass);

        try {
            GetPresignedObjectUrlArgs var3 = (GetPresignedObjectUrlArgs)((io.minio.GetPresignedObjectUrlArgs.Builder)((io.minio.GetPresignedObjectUrlArgs.Builder)GetPresignedObjectUrlArgs.builder().object(objectName)).bucket(bucketName)).expiry(expires).build();
            String var4 = minioClient.getPresignedObjectUrl(var3);
            return URLDecoder.decode(var4, "UTF-8");
        } catch (Exception var5) {
            log.error("文件路径获取失败" + var5.getMessage(), var5);
            return null;
        }
    }

    private static MinioClient initMinio(String minioUrl, String minioName, String minioPass) {
        if (minioClient == null) {
            try {
                minioClient = MinioClient.builder().endpoint(minioUrl).credentials(minioName, minioPass).build();
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return minioClient;
    }

    public static String upload(InputStream stream, String relativePath) throws Exception {
        initMinio(minioUrl, minioName, minioPass);
        if (minioClient.bucketExists((BucketExistsArgs)((BucketExistsArgs.Builder)BucketExistsArgs.builder().bucket(bucketName)).build())) {
            log.debug("Bucket already exists.");
        } else {
            minioClient.makeBucket((MakeBucketArgs)((io.minio.MakeBucketArgs.Builder)MakeBucketArgs.builder().bucket(bucketName)).build());
            log.debug("create a new bucket.");
        }

        PutObjectArgs var2 = (PutObjectArgs)((io.minio.PutObjectArgs.Builder)((io.minio.PutObjectArgs.Builder)PutObjectArgs.builder().object(relativePath)).bucket(bucketName)).contentType("application/octet-stream").stream(stream, (long)stream.available(), -1L).build();
        minioClient.putObject(var2);
        stream.close();
        return minioUrl + bucketName + "/" + relativePath;
    }
}
